﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using STS.Schema.Base;
using STS.Schema.Interfaces;
using STS.Common.Utils;
using STS.Common;
using STS.Schema.Enums;
using STS.Interfaces;
using System.Collections;

namespace STS.Schema.Factory.SqlServer
{
    public class SqlServer2008R2ObjectCollection<T> : SqlObjectCollection<T>, ISqlObjectCollection, ICollection, IList, IEnumerable
        where T : class, ISqlObject, new()
    {
        public SqlServer2008R2ObjectCollection(ISqlObject parent)
            : base(parent)
        {
        }

        public SqlServer2008R2ObjectCollection(ISqlObject parent, IEnumerable<T> collection)
            : base(parent, collection)
        {

        }

        public override string GetCollectionQuery(SqlObjectType? sqlObjectType = null, IDatabase database = null)
        {
            Type[] interfaceTypes = typeof(T).GetInterfaces();
            string dbUsage = database != null ? string.Format("USE {0};", database.Name) : null;
            if (interfaceTypes.Contains(typeof(IDatabase)))
            {
                return string.Concat(dbUsage, ResManager.GetStringResource("STS.Schema.Factory.SqlServer._2008R2.Scripts.selectDatabases.sql"));
            }
            if (interfaceTypes.Contains(typeof(ITable)))
            {
                return string.Concat(dbUsage, ResManager.GetStringResource("STS.Schema.Factory.SqlServer._2008R2.Scripts.selectTables.sql"));
            }
            if (interfaceTypes.Contains(typeof(IColumn)))
            {
                return string.Concat(dbUsage, ResManager.GetStringResource("STS.Schema.Factory.SqlServer._2008R2.Scripts.selectColumns.sql"));
            }
            if (interfaceTypes.Contains(typeof(IView)))
            {
                return string.Concat(dbUsage, ResManager.GetStringResource("STS.Schema.Factory.SqlServer._2008R2.Scripts.selectViews.sql"));
            }
            if (interfaceTypes.Contains(typeof(IForeignKey)))
            {
                return string.Concat(dbUsage, ResManager.GetStringResource("STS.Schema.Factory.SqlServer._2008R2.Scripts.selectForeignKeys.sql"));
            }
            if (interfaceTypes.Contains(typeof(IForeignKeyColumn)))
            {
                return string.Concat(dbUsage, ResManager.GetStringResource("STS.Schema.Factory.SqlServer._2008R2.Scripts.selectForeignKeyColumns.sql"));
            }
            if (interfaceTypes.Contains(typeof(IStoredProcedure)))
            {
                return string.Concat(dbUsage, ResManager.GetStringResource("STS.Schema.Factory.SqlServer._2008R2.Scripts.selectStoredProcedures.sql"));
            }
            if (interfaceTypes.Contains(typeof(IScalarFunction)))
            {
                return string.Concat(dbUsage, ResManager.GetStringResource("STS.Schema.Factory.SqlServer._2008R2.Scripts.selectScalarFunctions.sql"));
            }
            if (interfaceTypes.Contains(typeof(ITableValuedFunction)))
            {
                return string.Concat(dbUsage, ResManager.GetStringResource("STS.Schema.Factory.SqlServer._2008R2.Scripts.selectTableFunctions.sql"));
            }
            if (interfaceTypes.Contains(typeof(ITrigger)))
            {
                return string.Concat(dbUsage, ResManager.GetStringResource("STS.Schema.Factory.SqlServer._2008R2.Scripts.selectTriggers.sql"));
            }
            throw new STSException(string.Format("Unknown server object {0}", typeof(T).Name));
        }
    }
}
